<?php

namespace App\Http\Controllers\Api\V1;

use App\Models\TypeVideo;
use App\Models\Video;
use Illuminate\Http\Request;

class VideosController extends Controller
{
    public function index(Request $request)
    {
        $builder = Video::query();
        // 根据分类ID查询
        if ($type_id = $request->input('type_id')) {
            $videoIds = TypeVideo::query()->where('type_id', $type_id)->pluck('video_id')->toArray();

            $builder->whereIn('id', $videoIds);
        }
        // 根据关键字查询
        if ($keyword = $request->input('keyword')) {
            $builder->where(function ($query) use ($keyword) {
                $query->where('title', 'like', "%{$keyword}%");
            });
        }

        $videos = $builder
            ->latest('created_at')
            ->latest('id')
            ->paginate($request->input('perPage', 15), ['*'], 'page', $request->input('page', 1));

        return $this->success($videos);
    }

}
